package com.wellview.template.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;

import com.tencent.smtt.export.external.extension.interfaces.IX5WebSettingsExtension;
import com.tencent.smtt.export.external.extension.interfaces.IX5WebViewExtension;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.wellview.template.R;
import com.wellview.template.utils.NetTools;

import java.io.File;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/5.
 */

public class X5WebView extends WebView {

    private GenerateCallBack generateCallBack;

    public X5WebView(Context context) {
        this(context, null,0);
    }

    public X5WebView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet,0);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init(context);
        this.setWebViewClient(client);
        this.setWebChromeClient(chromeClient);
    }


    private WebViewClient client = new WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView webView, int i, String s, String s1) {
            if (generateCallBack == null){
                super.onReceivedError(webView, i, s, s1);
            }
           else {
                generateCallBack.onReceivedError(webView, i, s, s1);
            }
        }
    };

    private WebChromeClient chromeClient = new WebChromeClient(){

        @Override
        public Bitmap getDefaultVideoPoster() {
            return BitmapFactory.decodeResource(getResources(), R.drawable.app_start);
        }

        @Override
        public void onProgressChanged(WebView webView, int i) {
            if (generateCallBack == null){
                super.onProgressChanged(webView, i);
            }else {
                generateCallBack.callProcess(webView,i);
            }
        }

    };

    public void  init(Context context){
        WebSettings settings = this.getSettings();
        settings.setDisplayZoomControls(false);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);// 不支持缩放
//        settings.setUseWideViewPort(false);// 将图片调整到适合webview大小
//        settings.setLoadWithOverviewMode(false);// 缩放至屏幕的大小
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(false);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setAppCacheMaxSize(15 * 1024 * 1024);
        settings.setAppCachePath(context.getFilesDir() + File.separator + "web_cache");
        settings.setAllowFileAccess(true);
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        this.setVerticalScrollbarOverlay(false);
        this.setVerticalScrollBarEnabled(false);
        this.setHorizontalScrollBarEnabled(false);
        IX5WebSettingsExtension x5Setting = this.getSettingsExtension();
        if (x5Setting != null) {
            x5Setting.setPreFectch(true);
//            x5Setting.setRecordRequestEnabled(true);
        }
        IX5WebViewExtension x5WebSetting = this.getX5WebViewExtension();
        if (x5WebSetting != null) {
            x5WebSetting.setScrollBarFadingEnabled(false);
            x5WebSetting.setVerticalScrollBarEnabled(false);
        }

    }


    public void setGenerateCallBack(GenerateCallBack callBack){
        this.generateCallBack = callBack;
    }
    public  interface   GenerateCallBack{
        void callProcess(WebView webView,int progress);
        void onReceivedError(WebView webView, int i, String s, String s1);
    }
}

